package com.web.server.common.utils;

import com.google.gson.Gson;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by LILEI6 on 2016/8/11.
 */
@Component
public class EmailUtil {
    private static Logger logger = LoggerFactory.getLogger(EmailUtil.class);
    private static final String EMAIL_SERVER_URL = ConfigUtil.getProperty("email.url");

    public static void send(String receiver, String subject, String body) {
        Email email = new Email(receiver, subject, body);
        logger.info(String.format("Send-Email-Request: [url=%s, to=%s, subject=%s, body=%s]",
                EMAIL_SERVER_URL, email.receiver, email.subject, email.body));

        HttpResponse response = HttpRequest
                .post(EMAIL_SERVER_URL).multipart(true)
                .form("sendEmailInfo", new Gson().toJson(email))
                .send();
        logger.info(String.format("Send-Email-Response: [response=%s]", response.bodyText()));
    }

    private static class Email{
        private String receiver;   //多收件人逗号分隔
        private String subject;
        private String body;

        public Email(String receiver, String subject, String body) {
            this.receiver = receiver;
            this.subject = subject;
            this.body = body;
        }

        public Email(String receiver, String cc, String subject, String body) {
            this.receiver = receiver;
            this.subject = subject;
            this.body = body;
        }

        public String getReceiver() {
            return receiver;
        }

        public void setReceiver(String receiver) {
            this.receiver = receiver;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }
    }
}
